<?php
namespace BoxSns\Logic;
class MsgLogic extends \LogicController
{

    /**
     *$to_id 收信人
     *$content 信息内容
     */
    public function send($param)
    {
        $to_user_id = $param['to_user_id'];
        $content = $param['content'];
        $user_id = $param['user_id'];
        $read = 1;
        $user_id = $user_id ? $user_id : $this->user_id;
        $error = Logic("BoxSns", "Member")->check($this->user_id);
        if ($error['error'] > 0) {
            return $error;
        }
        $_message = BoxModel("SnsMessage");
        $content = strip_tags($content);
        $content = autolink($content);
        $msg_id = $_message->send($user_id, $to_user_id, $content);//发送信息
        /******消息发送成功之后的处理***/
        if (!$msg_id) {
            return false;
        }
        if ($read) {
            $this->msg_count($to_user_id, $user_id);            //未读消息数加1
        }
        $data = array(
            'user_id' => $user_id,
            'id' => $msg_id,
            'to_user_id' => $to_user_id,
            'content' => $content,
        );
        return $data;
    }

    /*******消息提醒数加1******/
    public function msg_count($user_id, $f_user_id)
    {
        $model = BoxModel("sns_message_count");
        $row = array(
            'user_id' => $user_id,
            'f_user_id' => $f_user_id,
        );
        $r = $model->where($row)->field("id")->find();
        if ($r) {
            $bool = $model->add($row);
        } else {
            $bool = $model->where($row)->setInc("messages_count");
        }
        BoxModel("SnsNotificationCount")->inc($user_id, "message");
        return $bool;
    }

    /*******我的对话***/
    public function message($param)
    {
        $error = Logic("BoxSns", "Member")->check($this->user_id);
        if ($error['error'] > 0) {
            return $error;
        }
        $limit = $param['limit'] ? $param['limit'] : 10;
        $where = "(user_id=$this->user_id  or to_user_id=$this->user_id)";
        $data = BoxModel("sns_message_record")->get($where, $limit, "id desc");
        $system_member = BoxModel("system_member");
        $sns_message = BoxModel("sns_message");
        foreach ($data['list'] as $k => $one) {
            $user_id = ($one['user_id'] == $user_id) ? $one['user_id'] : $one['to_user_id'];
            $data['list'][$k]['from_user_info'] = $system_member->load($user_id);
            $box_id = $one['box_id'];
            $data['list'][$k]['messgae'] = $sns_message->where("box_id='$box_id'")->find();
        }
        //dump($data);
        return $data;
    }

    /*******我的对话***/
    public function show_message($param)
    {
        $to_user_id = $param['to_user_id'];
        $error = Logic("BoxSns", "Member")->check($this->user_id);
        if ($error['error'] > 0) {
            return $error;
        }
        $box_id = ($to_user_id > $this->user_id) ? ($this->user_id . "_" . $to_user_id) : ($to_user_id . "_" . $this->user_id);
        $limit = $param['limit'] ? $param['limit'] : 10;
        $where = "box_id='$box_id'";
        $data = BoxModel("sns_message")->get($where, $limit, "id desc");
        $system_member = BoxModel("system_member");
        foreach ($data['list'] as $k => $one) {
            $user_id = ($one['user_id'] == $this->user_id) ? "" : $one['user_id'];
            $data['list'][$k]['headimg'] = $this->user_info['headimg'];
            $data['list'][$k]['me'] = "msg_box_right";
            if ($user_id) {
                $data['list'][$k]['from_user_info'] = $system_member->load($user_id);
                $data['list'][$k]['me'] = "";
                $data['list'][$k]['headimg'] = $data['list'][$k]['from_user_info']['headimg'];
                $data['list'][$k]['nickname'] = $data['list'][$k]['from_user_info']['nickname'];
            }

        }
        return $data;
    }


}
  
  